import React, { useMemo, useContext, useCallback } from 'react';

import SalaryPageProvider from '@/pages/SalaryManage/components/SalaryPageWrapper/SalaryPageProvider';
import { BUTTON } from '@/pages/SalaryManage/SalaryCalculate/constant/recheckButtonConstant';

import { message } from 'antd';
import PayoffCheck from './components/PayoffCheck';
import PayoffExport from './components/PayoffExport';
import PassCheck from './components/PassCheck';
import PassRejectCheck from './components/PassRejectCheck';
import PassFinalCheck from './components/PassFinalCheck';
import PassFinalRejectCheck from './components/PassFinalRejectCheck';
import ApproveSubmit from './components/ApproveSubmit';
import SendSalaryPayroll from './components/SendSalaryPayroll';
import ApplyPayment from './components/ApplyPayment';
import UpdateCheck from './components/UpdateCheck';
import UpdateCheckFn from './components/UpdateCheckFn';
import BatchReject from './components/BatchReject';
import BatchApprove from './components/BatchApprove';
import BatchApproveFn from './components/BatchApproveFn';
import useArticleWage from './components/useArticleWage';

/**
 *薪资模块按钮事件
 * @param checkList
 * @param actionType
 */
const useCheckButton = (checkList = [], actionType = 1, paymentTime) => {
    const { period, organizationCode, searchModel, updateSignal, selectedRows, changeSalaryProviderParam } = useContext(
        SalaryPageProvider
    );

    // 过滤按钮
    const checkButtonList = useMemo(() => checkList.filter(item => item && !item.disabled), [checkList]);

    const year = useMemo(() => period && period.substr(0, 4), [period]);
    const month = useMemo(() => period && period.substr(5), [period]);

    const { openArticleModal, render: articleRender } = useArticleWage(
        () => changeSalaryProviderParam({ updateSignal: !updateSignal }),
        {
            year,
            month,
            organizationCode
        }
    );

    // 驱动页面更新数据事件
    const updateStateFn = useCallback(
        res => {
            console.log(res);
            if (res && res.code === 10000 && res.data && res.data.responseCode) {
                openArticleModal(res.data.msg, res.data.responseCode === 10000);
                return;
            }
            if (res) {
                message.success(res.message || '操作成功');
            }
            changeSalaryProviderParam({
                updateSignal: !updateSignal
            });
        },
        [changeSalaryProviderParam, updateSignal, openArticleModal]
    );

    return {
        render() {
            return (
                <>
                    {articleRender()}
                    {checkButtonList.map(item => {
                        const { code } = item;
                        switch (code) {
                            case BUTTON.PO_PAYOFF:
                                // 执行付款
                                return (
                                    <PayoffCheck
                                        handleItem={item}
                                        year={year}
                                        month={month}
                                        organizationCode={organizationCode}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                    />
                                );
                            case BUTTON.PO_PAYOFF_EXCEL_EXPORT:
                                // 薪资发放Excel导出
                                return (
                                    <PayoffExport
                                        handleItem={item}
                                        period={period}
                                        paymentTime={paymentTime}
                                        organizationCode={organizationCode}
                                        departmentCode={searchModel?.departmentCode}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                    />
                                );
                            case BUTTON.OV_PASS:
                                // 复审通过
                                return (
                                    <PassCheck
                                        actionType={actionType}
                                        handleItem={item}
                                        year={year}
                                        month={month}
                                        organizationCode={organizationCode}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                    />
                                );
                            case BUTTON.OV_REJECT:
                                // 复审驳回
                                return (
                                    <PassRejectCheck
                                        actionType={actionType}
                                        handleItem={item}
                                        year={year}
                                        month={month}
                                        period={period}
                                        organizationCode={organizationCode}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                    />
                                );
                            case BUTTON.OV_FINAL_PASS:
                                // 终审通过
                                return (
                                    <PassFinalCheck
                                        actionType={actionType}
                                        handleItem={item}
                                        year={year}
                                        month={month}
                                        period={period}
                                        organizationCode={organizationCode}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                    />
                                );
                            case BUTTON.OV_FINAL_REJECT:
                                // 终审驳回
                                return (
                                    <PassFinalRejectCheck
                                        actionType={actionType}
                                        handleItem={item}
                                        year={year}
                                        month={month}
                                        period={period}
                                        organizationCode={organizationCode}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                    />
                                );
                            case BUTTON.APPROVE_SUBMIT:
                                // 提交审批
                                return (
                                    <ApproveSubmit
                                        handleItem={item}
                                        period={period}
                                        organizationCode={organizationCode}
                                        key={item.code}
                                        searchModel={searchModel}
                                    />
                                );
                            case BUTTON.SEND_SALARY_PAYROLL:
                                // 下发薪资单
                                return (
                                    <SendSalaryPayroll
                                        handleItem={item}
                                        period={period}
                                        organizationCode={organizationCode}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                        selectedRows={selectedRows}
                                    />
                                );
                            case BUTTON.APPLY_PAYMENT:
                                // 申请付款
                                return (
                                    <ApplyPayment
                                        handleItem={item}
                                        year={year}
                                        month={month}
                                        organizationCode={organizationCode}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                        selectedRows={selectedRows}
                                    />
                                );
                            case BUTTON.BATCH_APPROVE:
                                // HR批量通过
                                return (
                                    <BatchApprove
                                        handleItem={item}
                                        year={year}
                                        month={month}
                                        organizationCode={organizationCode}
                                        selectedRows={selectedRows}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                    />
                                );
                            case BUTTON.FN_BATCH_APPROVE:
                                // 财务批量通过
                                return (
                                    <BatchApproveFn
                                        handleItem={item}
                                        year={year}
                                        month={month}
                                        organizationCode={organizationCode}
                                        selectedRows={selectedRows}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                    />
                                );
                            case BUTTON.UPDATE_DATA:
                                // Hr更新数据
                                return (
                                    <UpdateCheck
                                        handleItem={item}
                                        year={year}
                                        month={month}
                                        organizationCode={organizationCode}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                        selectedRows={selectedRows}
                                    />
                                );
                            case BUTTON.FN_UPDATE_DATA:
                                // 财务更新数据
                                return (
                                    <UpdateCheckFn
                                        handleItem={item}
                                        year={year}
                                        month={month}
                                        organizationCode={organizationCode}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                        selectedRows={selectedRows}
                                    />
                                );
                            case BUTTON.OV_FINAL_BATCH_REJECT:
                                // 全量驳回
                                return (
                                    <BatchReject
                                        handleItem={item}
                                        year={year}
                                        month={month}
                                        organizationCode={organizationCode}
                                        key={item.code}
                                        updateStateFn={updateStateFn}
                                        selectedRows={selectedRows}
                                    />
                                );
                            default:
                                return null;
                        }
                    })}
                </>
            );
        }
    };
};

export default useCheckButton;
